package cn.controller;

import cn.service.impl.BrandServiceImpl;
import entity.Brand;
import entity.Result;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class BrandController {
    @Resource
    BrandServiceImpl brandService;

    @RequestMapping("/queryAllBrand")
    public Result queryAllBrand(Integer currentPage,Integer pageSize) {
        return brandService.queryAllBrand(currentPage,pageSize);
    }

    @RequestMapping("/updateIs_show")
    public void updateIs_show(Integer brand_id,Integer is_show) {
        brandService.updateIs_show(brand_id,is_show);
    }

    @RequestMapping("/updateIs_manufacturer")
    public void updateIs_manufacturer(Integer brand_id,Integer is_manufacturer) {
        brandService.updateIs_manufacturer(brand_id,is_manufacturer);
    }

    @RequestMapping("/batchDelete")
    public Result batchDelete(@RequestBody List<Brand> brandList) {
        //获取品牌编号的集合
        List<Integer> brandLists = new ArrayList<>();
        for (Brand brand : brandList) {
            brandLists.add(brand.getBrand_id());
        }
        return brandService.deleteBrandByBrand_id(brandLists);
    }

    @RequestMapping("/queryBrand")
    public Result queryBrand(String brand_name) {
        return brandService.queryBrandByBrand_name(brand_name);
    }

    @RequestMapping("/logoUpload")
    public Result logoUpload(MultipartFile multipartFile) {
        //将图片存入后端,并返回一个图片地址
        try {
            String path = ResourceUtils.getURL("classpath:").getPath() + "static/images/";
            File file = new File(path);
            if (!file.exists()) {
                file.mkdirs();
            }
            InputStream inputStream = multipartFile.getInputStream();
            String fileName = multipartFile.getOriginalFilename();
            fileName = System.currentTimeMillis() + fileName;
            OutputStream outputStream = new FileOutputStream(path + fileName);
            byte[] b = new byte[1024 * 1024];
            int d = -1;
            while ((d = inputStream.read(b)) != -1) {
                outputStream.write(b,0,d);
                outputStream.flush();
            }
            outputStream.close();
            String fileUrl = "http://localhost:8080/images/" + fileName;
            return Result.success(fileUrl);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("/areaPicUpload")
    public Result areaPicUpload(MultipartFile multipartFile) {
        return logoUpload(multipartFile);
    }

    @RequestMapping("/addBrand")
    public Result addBrand(@RequestBody Brand brand) {
        return brandService.addBrand(brand);
    }

    @RequestMapping("/deleteRow")
    public Result deleteRow(Integer brand_id) {
        return brandService.deleteByPrimaryKey(brand_id);
    }

    @RequestMapping("/queryByBrand_Id")
    public Result queryByBrand_Id(Integer brand_id) {
        return brandService.selectByPrimaryKey(brand_id);
    }

    @RequestMapping("/updateBrand")
    public Result updateBrand(@RequestBody Brand brand) {
        System.out.println(brand);
        return brandService.updateBrand(brand);
    }
}
